home *** CD-ROM | disk | FTP | other *** search
- /* @(#) $Header: stime.c,v 1.6 91/05/24 12:10:16 deyke Exp $ */
-
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
-
- #include "global.h"
- #include "mbuf.h"
- #include "netuser.h"
- #include "timer.h"
- #include "udp.h"
-
- #define PORT 4713
- #define POSTFIX 0xaaaa5555
- #define PREFIX 0x5555aaaa
-
- struct stime {
- int32 prefix;
- struct timeval tv;
- struct timezone tz;
- int32 chksum;
- int32 postfix;
- };
-
- struct dests {
- int32 addr;
- struct dests *next;
- };
-
- static int err;
- static int rcvd;
- static int sent;
- static struct dests *dests;
- static struct timer timer;
- struct udp_cb *ucb;
-
- /*---------------------------------------------------------------------------*/
-
- static void stime_recv(iface, up, cnt)
- struct iface *iface;
- struct udp_cb *up;
- int cnt;
- {
-
- struct mbuf *bp;
- struct socket fsock;
- struct stime stime;
-
- recv_udp(up, &fsock, &bp);
- if (pullup(&bp, (char *) &stime, sizeof(struct stime )) != sizeof(struct stime )) goto discard;
- if (stime.prefix != PREFIX || stime.postfix != POSTFIX) goto discard;
- if (stime.tv.tv_sec + stime.tv.tv_usec + stime.tz.tz_minuteswest + stime.tz.tz_dsttime != stime.chksum) goto discard;
- settimeofday(&stime.tv, &stime.tz);
- rcvd++;
- free_p(bp);
- return;
-
- discard:
- err++;
- free_p(bp);
- }
-
- /*---------------------------------------------------------------------------*/
-
- static void stime_send(arg)
- void *arg;
- {
-
- struct dests *pd;
- struct mbuf *bp;
- struct socket fsock, lsock;
- struct stime stime;
-
- lsock.address = INADDR_ANY;
- lsock.port = fsock.port = PORT;
- stime.prefix = PREFIX;
- stime.postfix = POSTFIX;
- for (pd = dests; pd; pd = pd->next) {
- fsock.address = pd->addr;
- bp = alloc_mbuf(sizeof(struct stime ));
- bp->cnt = sizeof(struct stime );
- gettimeofday(&stime.tv, &stime.tz);
- stime.chksum = stime.tv.tv_sec + stime.tv.tv_usec + stime.tz.tz_minuteswest + stime.tz.tz_dsttime;
- memcpy(bp->data, &stime, sizeof(struct stime ));
- send_udp(&lsock, &fsock, 0, 0, bp, 0, 0, 0);
- sent++;
- }
- set_timer(&timer, 3600 * 1000L);
- start_timer(&timer);
- }
-
- /*---------------------------------------------------------------------------*/
-
- int dostime(argc, argv, p)
- int argc;
- char *argv[];
- void *p;
- {
-
- int32 addr;
- struct dests *pd;
-
- if (argc < 2) {
- printf("sent %d rcvd %d err %d\n", sent, rcvd, err);
- if (ucb) puts("Receiver active");
- if (dests) {
- puts("Destinations:");
- for (pd = dests; pd; pd = pd->next)
- printf(" %s\n", inet_ntoa(pd->addr));
- }
- return 0;
- }
-
- if (!strcmp(argv[1], "clear")) {
- while (pd = dests) {
- dests = dests->next;
- free(pd);
- }
- return 0;
- }
-
- if (!strcmp(argv[1], "start")) {
- if (!ucb) {
- struct socket sock;
- sock.address = INADDR_ANY;
- sock.port = PORT;
- ucb = open_udp(&sock, stime_recv);
- }
- return 0;
- }
-
- if (!strcmp(argv[1], "stop")) {
- if (ucb) {
- del_udp(ucb);
- ucb = 0;
- }
- return 0;
- }
-
- if (addr = resolve(argv[1])) {
- for (pd = dests; pd; pd = pd->next)
- if (addr == pd->addr) return 0;
- pd = (struct dests *) malloc(sizeof(struct dests ));
- pd->addr = addr;
- pd->next = dests;
- dests = pd;
- timer.func = stime_send;
- timer.arg = 0;
- set_timer(&timer, 3600 * 1000L);
- start_timer(&timer);
- return 0;
- }
-
- return (-1);
- }
-
-